﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Treasuries.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Treasuries.Payments
{
    public class AdvancePaymentOrderLinkRepository : EfCoreRepository<FinanceDbContext, PaymentOrderLink, Guid>, IPaymentOrderLinkRepository
    {
        public AdvancePaymentOrderLinkRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<long> GetCountAsync(Guid? orderId, Guid? paymentId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var count = await dbContext.Set<PaymentOrderLink>().AsNoTracking()
                .WhereIf(orderId.HasValue, x => x.OrderId == orderId)
                .WhereIf(paymentId.HasValue, x => x.PaymentId == paymentId)
                .CountAsync(cancellationToken);

            return count;
        }

        public async Task<List<PaymentOrderLink>> GetListAsync(Guid? orderId, Guid? paymentId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var entities = await dbContext.Set<PaymentOrderLink>().AsNoTracking()
                .WhereIf(orderId.HasValue, x => x.OrderId == orderId)
                .WhereIf(paymentId.HasValue, x => x.PaymentId == paymentId)
                .ToListAsync(cancellationToken);

            return entities;
        }
    }
}
